const News = require('../model/news.model');
class NewsService {
    // 按照新闻的名称进行查询
    async getNews(newsname) {
        const res = await News.findOne({ where: { newsname } });
        return res;
    }
    // 新闻添加
    async newsAdd(news) {
        const res = await News.create(news);
        return res.dataValues;
    }
    // 新闻修改
    async newsUpdate(news) {
        const res = await News.update(news, { where: { id: news.id } });
        return res[0] > 0;
    }
    // 新闻删除
    async newsDelete(id) {
        const res = await News.destroy({ where: { id } });
        return res > 0;
    }
    // 新闻详情
    async newsDetail(id) {
        const res = await News.findByPk(id);
        return res ? res.dataValues : null;
    }
    // 新闻列表
    async newsList(page = 1, pageSize = 10, otherNewsOptions = {}) {
        const offset = (page - 1) * pageSize
        const options = {
            offset,
            limit: pageSize,
        };
        if (otherNewsOptions.newsname != '' && otherNewsOptions.newsname != null && otherNewsOptions.newsname) {
            options.where = {
                newsname: otherNewsOptions.newsname // 根据 newsname 进行查询
            };
        }
        const res = await News.findAll(options);
        const total = await News.count();
        return {
            res, total
        };
    }
}
module.exports = new NewsService();